#pragma once

#include <iostream>
#include <string>
#include <unordered_map>
#include "Connection.hpp"
#include "Log.hpp"
#include "Multiplex.hpp"
using namespace ns_log;

class TcpServer
{
public:
    TcpServer() : _epoller(std::make_unique<Epoller>())
    {
    }
    void AddConnection(int fd, uint32_t events)
    {
        // 1.构建connection
        Connection *newcon = new Connection(fd);
        newcon->SetEvents(events);

        // 2.添加进epoll--即内核中
        if(!_epoller->AddEvents(newcon->GetSockFd(), newcon->GetEevents()))
        {
            return;
        }

        // 3.添加进_connections；
        _connections.insert(std::make_pair(newcon->GetSockFd(), newcon));
    }
    ~TcpServer()
    {
    }

private:
    std::unordered_map<int, Connection *> _connections; // 管理所有的Connection
    std::unique_ptr<Multiplex> _epoller;
};